
package Ex;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXLocator_Ex10 extends DefaultHandler {

    private Locator locator;

    public SAXLocator_Ex10() {

    }

    public void setDocumentLocator(Locator locator) {
        this.locator = locator;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equals("user")) {
            System.out.println("Process element start.");
        }
        else{
            String location = "";
            if (locator != null) {
                location = locator.getPublicId();
                location+=" line " + locator.getLineNumber()
                        + " column " + locator.getColumnNumber();
            }
            System.out.println("location: " + location);
        }
    }

    ///
    public static void main(String[] args) throws SAXException {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setValidating(true);
        try {
            SAXParser parser = spf.newSAXParser();
            parser.parse(new File("tomcat-users.xml"), new SAXLocator_Ex10());
        } catch (SAXException ex) {
            ex.printStackTrace();
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
            System.exit(1);
        }catch(IOException e){
            e.printStackTrace();
        }
    }

}
